package com.jackrain.nea.omslocal.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.omslocal.conf.LoadCloumnsConfig;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Discription: 换货功能中, 卖家确认收货后, 发货
 * 将确认收货数据 发送到线上, 调用收货接口
 * Created with IntelliJ IDEA.
 * @Author cs
 * @Date 2018/1/9 10:14
 */
@Slf4j
@Component
public class ExchangeRefundConfirmService {


    @Autowired
    private SendMqServies sendMq;
    @Autowired
    private LoadCloumnsConfig loadCloumnsConfig;
    @Autowired
    private PropertiesConf pconf;
    @Autowired
    private LocalLogService localLogService;


    /**
     * 卖家确认收货启动方法
     */
    public void exchangeStart() {
        // 获取自身 bean
        ExchangeRefundConfirmService exchangeRefundConfirmService = ApplicationContextHandle.getBean(ExchangeRefundConfirmService.class);
        exchangeRefundConfirmService.exchangeConfirm();
    }

    /**
     * 循环遍历确认收货视图
     *
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void exchangeConfirm() {

        try {
            // 查询上限
            int rownums = pconf.getPropertyInt("ons.pushorderNum");
            // 查询确认收货视图数据
            String countSql = "select * from t_v_oms_returngoodConfirm where returngoodconfirm = 0 and  rownum <= " + rownums + " order by " +
                    "created for update skip locked";
            JSONArray jsonArr = QueryEngine.getInstance().doQueryObjectArray(countSql);
            // 有需要同步的数据 将查询到的数据以消息形式发送
            if (jsonArr == null || jsonArr.isEmpty()) {
                log.debug("returngoodConfirm  Order jsonArr is null");
                return;
            }

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("data", jsonArr.toJSONString());
            jsonObject.put("tag", pconf.getProperty("ons.comp"));
            String msgId = sendMq.sendMqinfoObj(jsonObject, pconf.getProperty("ons.exchangeconfirmTopic", "TAOBAO_EXCHANGE_CONFIRM"), pconf.getProperty("ons.comp", "burgeon"));

            log.debug("卖家确认收货：jsonArr= {}, msgId= {}", jsonArr.toString(), msgId);
            // 修改状态
            this.changeStatus(jsonArr);
        } catch (Exception e) {
            //  同步发货出现异常
            log.error("同步发货订单失败");

        }

    }


    /**
     * 发送之后修改状态
     */
    public void changeStatus(JSONArray jsonArray) {
        // returngoodconfirm = 9时,该信息已经发送
        String sql = "update t_v_oms_returngoodConfirm set returngoodconfirm = 9 where dispute_id = ? ";
        JSONArray paramArr = new JSONArray();
        Long dispute_id = 0L;
        try {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject tempObj = JSONObject.parseObject(jsonArray.get(i).toString());
                dispute_id = tempObj.getLong("DISPUTE_ID");
                QueryEngine.getInstance().executeUpdate(sql, new Object[]{dispute_id});
            }

        } catch (Exception e) {
            // 修改库存状态失败
            log.error("修改换货卖家确认视图状态为已发送失败" + dispute_id);
        }

    }


}
